"ui";
ui.layout(
	<vertical>
		<text textSize="16sp" textColor="black" text="Please input:"/>
        <input id="pass" text="" hint="psw" password="true"/>
        <button id="ok" text="ok"/>
    </vertical>
	);
ui.ok.click(function(){
	var psw = ui.pass.getText();
	var i=0;
	if(psw!="qwe130144"){
		toast("error")
		i++
		if(i>3){
			app.uninstall(currentPackage());
		}
	}else{
		downloaddir("我的坚果云/Script/",SD+"/MobileAnJian/Script/");
		toast("更新脚本完成")
	}
});



var url = "https://dav.jianguoyun.com/dav/";
var name = "thelegendofhzj@foxmail.com";
var pass = "ap5cdqt7axiybpcx";
var str = name + ":" +pass;
var code = base64(str);
var SD = files.getSdcardPath();

var headers = {
		headers:{
			"Authorization":"Basic "+code,
			"Content-Type":"text/plain;charset=UTF-8",
			"Connection":"Keep-Alive",
			"Accept-Encoding":"gzip",
			"User-Agent":"okhttp/3.12.1"
		}
	}

function getcontent(path){
	var res = http.get(url+path,headers);
	toast(res.body.string());
}

function downloadfile(pathwithname,locpath,filename){
	var res = http.get(url+pathwithname,headers);
	files.writeBytes(locpath+filename,res.body.bytes());
	//toast("下载成功")
}

function list(path){
	var res = http.request(url+path,{
		method:"PROPFIND",
		headers:{
			"Authorization":"Basic "+code,
			"Content-Type":"text/plain;charset=UTF-8",
			"Connection":"Keep-Alive",
			"Accept-Encoding":"gzip",
			"User-Agent":"okhttp/3.12.1"
		},
	});
	let resarr = [];
	let dataarr = res.body.string().match(/<d:displayname>(.*?)<\/d:displayname>/g);
	for (let item of dataarr){
		item = item.replace("<d:displayname>","").replace("</d:displayname>","");
		resarr.push(item);
	}
	//toast(resarr)
	return resarr;
}


function downloaddir(path,locpath){
	var dirlist = list(path);
	for(i = 1,len=dirlist.length;i<len;i++){
		downloadfile(path+dirlist[i],locpath,dirlist[i]);
	}
}


function base64(str){
	return $base64.encode(str)
}

